package net.mikoo.seals.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.api.model.ResultType;
import net.mikoo.seals.api.model.verify.VerifyDetailRequest;
import net.mikoo.seals.api.model.verify.VerifyDetailResponse;
import net.mikoo.seals.api.model.verify.VerifyListRequest;
import net.mikoo.seals.api.model.verify.VerifyListResponse;
import net.mikoo.seals.api.model.verify.VerifyRequest;
import net.mikoo.seals.api.model.verify.VerifyResponse;
import net.mikoo.seals.api.utility.PartyUtility;
import net.mikoo.seals.biz.service.VerifyService;
import net.mikoo.seals.common.enums.ApplyStatusEnum;
import net.mikoo.seals.common.enums.VerifyStatusEnum;
import net.mikoo.seals.common.exception.NeedBackMsgException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.ApplyVerifyQuery;

@Controller
public class VerifyController {

	private static Logger logger = LoggerFactory.getLogger(VerifyController.class);
	
	@Autowired
	private VerifyService verifyService;
	
	@RequestMapping("/verify/list")
	@ResponseBody VerifyListResponse list(VerifyListRequest request, HttpServletRequest httpRequest) {
		VerifyListResponse result = new VerifyListResponse();
		try {
			
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getPageNo() == null || request.getPageNo() < 1) {
				request.setPageNo(1);
			}
			
			if(request.getPageSize() == null || request.getPageSize() < 1) {
				request.setPageSize(10);
			}
			
			ApplyVerifyQuery query = new ApplyVerifyQuery();
			VerifyStatusEnum verifyStatus = VerifyStatusEnum.getByCode(request.getStatus());
			if(verifyStatus != null) {
				query.setVerifyStatus(verifyStatus.getCode());
			}
			query.setOffset((request.getPageNo() - 1)*request.getPageSize());
			query.setPageSize(request.getPageSize());
			query.setPartyId(PartyUtility.getCurrentPartyId(httpRequest));
			query.setCompanyId(PartyUtility.getCurrentPartyCompanyId(httpRequest));
			query.setVerifyStatus(VerifyStatusEnum.PENDING.getCode());
			PageModel<ApplyVerify> pm = verifyService.getPageByQuery(query);
			
			result = VerifyListResponse.valueOf(pm);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/verify/detail")
	@ResponseBody
	public VerifyDetailResponse detail(VerifyDetailRequest request , HttpServletRequest httpRequest) {
		VerifyDetailResponse result = new VerifyDetailResponse();
		try {
			
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getVerifyId() == null) {
				throw new IllegalArgumentException("request.verifyId");
			}
			
			ApplyVerify verify = verifyService.getVerifyDetail(request.getVerifyId());
			if(verify != null || PartyUtility.getCurrentPartyId(httpRequest).equals(verify.getApplyVerifyId())) {
				result = VerifyDetailResponse.valueOf(verify);
			}
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
	@RequestMapping("/verify/verify")
	@ResponseBody
	public VerifyResponse verify(VerifyRequest request, HttpServletRequest httpRequest) {
		VerifyResponse result = new VerifyResponse();
		try {
			
			if(request == null) {
				throw new IllegalArgumentException("request");
			}
			
			if(request.getVerifyId() == null) {
				throw new IllegalArgumentException("request.getVerifyId");
			}
			
			VerifyStatusEnum verifyResult = VerifyStatusEnum.getByCode(request.getResult());
			if(verifyResult == null) {
				throw new IllegalArgumentException("request.getResult");
			}
			
			if(verifyResult == VerifyStatusEnum.REJECT && StringUtils.isBlank(request.getRemark())) {
				throw new IllegalArgumentException("request.getRemark");
			}
			
			ApplyVerify verify = new ApplyVerify();
			verify.setVerifyStatus(verifyResult.getCode());
			verify.setRemark(request.getRemark());
			verify.setPartyId(PartyUtility.getCurrentPartyId(httpRequest));
			verify.setCompanyId(PartyUtility.getCurrentPartyCompanyId(httpRequest));
			verify.setApplyVerifyId(request.getVerifyId());
			verifyService.verify(verify);
			result.setResult(ResultType.SUCCESS.getValue());
		}catch(NeedBackMsgException ne) {
			logger.error("", ne);
			result.setResult(ResultType.ERROR.getValue());
			result.setErrorMsg(ne.getMessage());
		}catch(Exception e) {
			logger.error("", e);
			result.setResult(ResultType.ERROR.getValue());
		}
		return result;
	}
	
}
